import { ExtractJwt, Strategy } from 'passport-jwt';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { PassportStrategy } from '@nestjs/passport';
import { ConfigService } from '@nestjs/config';
import { AdminUser } from '../../admin-users/entities/admin-user.entity';

type JwtPayload = Pick<AdminUser, 'userId' | 'roleId'> & {
	iat: number;
	exp: number;
};

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
	constructor(private jwtService: JwtService, private configService: ConfigService) {
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			secretOrKey: configService.get('auth.secret'),
		});
	}

	public validate(payload: JwtPayload) {
		if (!payload.userId) {
			throw new UnauthorizedException();
		}

		return payload;
	}
}
